package com.system.util.freemarker.template;

import com.component.domain.Content;
import com.component.domain.MChannel;
import com.component.domain.Note;
import com.component.service.ContentService;
import com.component.service.MChannelService;
import com.component.service.NoteService;
import com.system.common.ContextUtils;
import com.system.config.freemarker.DirectiveHandler;
import com.system.config.freemarker.FreemarkerScan;
import com.system.config.freemarker.TemplateDirective;
import com.system.util.freemarker.vo.ChannelTitle;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname ChannelTitleListTemplate
 * @Description TODO
 * @Date 2023/9/22 17:36
 * @Created by XiongXiong
 * @Author: X.I.O
 *
 * 栏目文章管理
 */
@FreemarkerScan("channeltitlelist")
public class ChannelTitleListTemplate extends TemplateDirective{

    @Autowired
    private MChannelService mChannelService;

    @Autowired
    private ContentService contentService;

    @Autowired
    private NoteService noteService;

    @Override
    public String getName() {
        return "channeltitlelist";
    }

    @Override
    public void execute(DirectiveHandler handler) throws Exception {
        Map<String,Object> obj=new HashMap<>();
        String channelpath=handler.getString("channelpath");
        Integer length=handler.getInteger("length");
        if(length==null){
            length=10;
        }
        MChannel mChannel=mChannelService.getMChannelByPath(channelpath);
        ChannelTitle channelTitle=new ChannelTitle();
        if(mChannel!=null) {
            int total = handler.getInteger("total");
            List<Note> list = noteService.channelList(mChannel.getId().toString(), total);
            channelTitle.setNotes(list);
            channelTitle.setChannelName(mChannel.getChannelname());
            channelTitle.setChannelPath(mChannel.getChannelpath());
        }
        handler.put(RESULT,channelTitle).render();
    }
}
